
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述: 修改昵称请求类
 *
 * @author jamywang
 * @since 2016年5月18日 下午8:53:13
 */
public class UpdateNickNameTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    public UpdateNickNameResponse request(String newNickName, String userName) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> params = new HashMap<String, Object>();
        HttpBaseRequestPackage requestPackage = new HttpBaseRequestPackage();
        ResponsePackage responsePackage = new UpdateNickNameResponsePackage();
        UpdateNickNameResponse responseObj = new UpdateNickNameResponse();
        requestPackage.setUserName(userName);
        params.put("cmd", ProtocolCmd.CMD_UPDATE_NICK_NAME);
        params.put("newnickname", newNickName);
        requestParams.add(params);
        requestPackage.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_UPDATE_NICK_NAME);
        try {
            KGHttpClient.request(requestPackage, responsePackage);
            responsePackage.getResponseData(responseObj);
            return responseObj;
        } catch (Exception e) {
        }
        return null;
    }

    private class UpdateNickNameResponsePackage extends
            HttpBaseResponsePackage<UpdateNickNameResponse> {

        @Override
        protected void handleResponse(UpdateNickNameResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "UpdateNickNameTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    if (ProtocolCmd.CMD_UPDATE_NICK_NAME.equalsIgnoreCase(cmd)) {
                        response.setOk(true);
                    }
                }

            } catch (Exception e) {
            }

        }

    }

    public static class UpdateNickNameResponse extends ResponseMessage {

    }

}
